package com.woniuxy.repay.controller;

import com.woniuxy.entity.RepaymentManage;
import com.woniuxy.repay.annotation.MonitorLog;
import com.woniuxy.repay.entity.PageBean;
import com.woniuxy.repay.service.RepaymentManageService;
import com.woniuxy.utils.ResponseResult;
import com.woniuxy.utils.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author shichen
 * @create 2021-11-13-10:40
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/manage")
public class RepaymentManageController {

    @Resource
    RepaymentManageService repaymentManageService;

    /**
     * 查询
     * @param pageIndex
     * @return
     */
    @MonitorLog("execute repaymentManage SelectAll")
    @GetMapping("/selectall/{pageIndex}")
    public ResponseResult selectAll(@PathVariable("pageIndex") Integer pageIndex, RepaymentManage repaymentManage) {
        // 校验
        log.debug("进入方法 selectAll request : {} ", repaymentManage);
        ResponseResult<PageBean> responseResult = null;
        try {
            PageBean pageBean = repaymentManageService.selectByCondition(repaymentManage, pageIndex);
            responseResult=new ResponseResult(200,ResultStatus.HAVE_PERMS,"search success!",pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = new ResponseResult(500, ResultStatus.NO_PERMS, "error", null);
        }

        log.debug("response : {} ", responseResult);


        return responseResult;
    }

    /**
     * 将传过来的信息，对数据库进行修改
     * @param repaymentManage
     * @return
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody RepaymentManage repaymentManage){
        log.debug("request : {}", repaymentManage);
        ResponseResult responseResult = null;
        try {
            repaymentManageService.updateByCondition(repaymentManage);
            responseResult=new ResponseResult(200,ResultStatus.NO_PERMS,"Update Success",null);
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = new ResponseResult(500, ResultStatus.NO_PERMS, "Update Error", null);
        }
        return responseResult;
    }

    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody RepaymentManage repaymentManage){
        log.debug("request : {}", repaymentManage);
        ResponseResult responseResult = null;
        try {
            repaymentManageService.insert(repaymentManage);
            responseResult=new ResponseResult(200,ResultStatus.NO_PERMS,"Insert Success",null);
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = new ResponseResult(500, ResultStatus.NO_PERMS, "Insert Error", null);
        }
        return responseResult;
    }
}
